package com.attendancesystem.controller;

import com.attendancesystem.model.Student;
import com.attendancesystem.util.JDBCUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;



import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.sql.*;

@WebServlet(name = "StudentInfoController", value = "/StudentInfoController")
public class StudentInfoController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取当前登录学生ID（示例从session获取）
        response.setContentType("text/html;charset=UTF-8");

        HttpSession session = request.getSession();
        String studentId = (String) session.getAttribute("username");

        // 数据库查询
        Student student = null;
        String sql = "SELECT * FROM students WHERE id = "+studentId;
        try ( Connection con = JDBCUtils.getConnection(); // 必须从连接池获取
              PreparedStatement stm = con.prepareStatement(sql)) {
            ResultSet res = stm.executeQuery();
            while (res.next()) {
                student = new Student(
                        res.getString("id"),
                        res.getString("name"),
                        res.getString("password"),
                        res.getString("college"),
                        res.getString("class")
                );
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
//        System.out.println("student:"+student);

        // 设置作用域对象
        request.setAttribute("student", student);
        request.getRequestDispatcher("/WEB-INF/views/Student.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);

    }
}
